/**
 * @class This static class implements manipulation of <select> html elements
 * @author erik 2/6/2007
 * @uses lib/Company/Company.js
 * @uses lib/Prototype/Prototype.js
 * @uses lib/Company/Utilities/Utils.js
 * @using Company.Error
 * @using Company.Error.ErrorHandler
 */

Company.UI.SelectUtils = {
    /**
    * The selected value of the select box
    */
    selectedValue: "",

    /**
    * Gets the selected value
    * @returns {String} the selected value
    */
    getSelectedValue: function() {
        return this.selectedValue;
    },

    /**
    * Sets the selected value
    * @param {String} value The value to set the selected value to
    */
    setSelectedValue: function(value) {
        this.selectedValue = value;
    },

    /**
    * Selects the first <option> of a <select> box with the given value or text
    * @param {HtmlElement} selectBox
    * @param {String} optionValue
    */
    selectOption: function(selectBox, optionValue) {
        selectBox = $(selectBox);
        optionValue = (optionValue + "").toLowerCase();

        for (var i = 0; i < selectBox.options.length; ++i) {
            if ((selectBox.options[i].value || selectBox.options[i].text).toLowerCase() == optionValue) {
                selectBox.selectedIndex = i;
                return;
            }
        }
    },

    /**
    * Empties a select box of all options
    * @param {HtmlElement} selectBox
    */
    clear: function(selectBox) {
        selectBox = $(selectBox);

        for (i = selectBox.length - 1; i >= 0; i--) {
            selectBox.remove(i);
        }
    },

    /**
    * Selects the first <option> of a <select> box with the given value or text
    * @param {HtmlElement} selectBox
    * @param {String} optionValue
    * @param {Array} initialOptions An array of objects that contains the initial options to be added 
    * to the select box. ie:
    *   initialOptions[0] = {value: '', text: 'Select one'};
    *   initialOptions[1] = {value: 'All', text: 'Select all'};
    */
    fillSelectBoxWithKeyValueXml: function(selectBox, xml, selectedValue, initialOptions) {
        this.fillSelectBoxWithXml(selectBox, xml, selectedValue, "Key", "Value", initialOptions);
    },


    /**
    * Fills a select box with options created from the passed xml
    * @param {HtmlElement} selectBox
    * @param {Xml} xml
    * @param {String} selectedValue the selected value
    * @param {String} valueField the xml field/node that is used for the option value
    * @param {String} textField the xml field/node that is used for the option text (what is displayed to the user)
    * @param {Array} initialOptions An array of objects that contains the initial options to be added 
    * to the select box. ie:
    *   initialOptions[0] = {value: '', text: 'Select one'};
    *   initialOptions[1] = {value: 'All', text: 'Select all'};
    */
    fillSelectBoxWithXml: function(selectBox, xml, selectedValue, valueField, textField, initialOptions) {
        selectBox = $(selectBox);
        this.clear(selectBox);
        this.setSelectedValue(selectedValue);

        var nodesValues = xml.getElementsByTagName(valueField);
        var nodesText = xml.getElementsByTagName(textField);

        if (initialOptions) {
            for (i = 0; i < initialOptions.length; i++) {
                this.addOption(selectBox,
                            initialOptions[i].value,
                            initialOptions[i].text
                            );
            }
        }
        if (!(nodesValues || nodesText)) {
            Company.Error.ErrorHandler.consoleError("Error in Company.UI.SelectUtils.fillSelectBoxWithXml: nodesValues or nodesText are empty");
            return;
        }

        if (nodesValues.length != nodesText.length) {
            Company.Error.ErrorHandler.consoleError("Error in Company.UI.SelectUtils.fillSelectBoxWithXml: nodesValues is not the same length as nodesText");
            return;
        }

        for (i = 0; i < nodesValues.length; i++) {
            this.addOption(selectBox,
                            Company.Utilities.getXmlNodeText(nodesValues[i]),
                            Company.Utilities.getXmlNodeText(nodesText[i])
                            );
        }
    },


    /**
    * Adds an option to a select box
    * @param {HtmlElement} selectBox
    * @param {String} valueItem the string for the option value
    * @param {String} textItem the string for the option text (what is displayed to the user)
    * @param {String} selectedValue the selected value
    */
    addOption: function(selectBox, valueField, textField, selectedValue) {
        selectBox = $(selectBox);
        selectedValue = selectedValue || this.getSelectedValue();

        var oOption = document.createElement("option");
        oOption.text = textField || "";
        oOption.value = valueField || "";

        if (oOption.value.toLowerCase() == selectedValue.toLowerCase()) {
            oOption.selected = true;
        }

        selectBox.options.add(oOption);
    }
}
